package com.ahwei.service.impl;

import com.ahwei.context.AppContext;
import com.ahwei.dao.user.UserDao;
import com.ahwei.dao.user.UserDetailDao;
import com.ahwei.pojo.User;
import com.ahwei.pojo.UserDetail;
import com.ahwei.pojo.enums.Result;
import com.ahwei.pojo.model.PageResult;
import com.ahwei.service.UserService;
import com.ahwei.util.MyBatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author：ahwei163@qq.com
 *
 * @date: 202*-03-14 20:34:43
 * @description: 这是一个Java文件！
 */
public class UserServiceImpl implements UserService {
    private final SqlSession sqlSession = MyBatisUtils.getSqlSession(true);
    private final UserDao userDao = sqlSession.getMapper(UserDao.class);
    private final UserDetailDao userDetailDao = sqlSession.getMapper(UserDetailDao.class);


    /**
     * 根据账号获取用户
     * @param account ·
     * @return ·
     */
    @Override
    public User findUserByAccount(String account) {
       return userDao.findUserByAccount(account);
    }
    /**
     * 根据账号获取用户
     * @param uid ·
     * @return ·
     */
    @Override
    public User findUserByUid(long uid) {
        return userDao.findUserByUid(uid);
    }

    @Override
    public int updateUser(User user) {
        return userDao.updateUser(user);
    }

    /**
     * 用户登录
     * @param account ·
     * @param password ·
     * @return ·
     */
    @Override
    public Result UserLogin(String account, String password) {
        //根据账号去查找用户
        User user = findUserByAccount(account);
        if (user != null){
            //如果user不为空，验证密码
            if (user.getPassword().equals(password)){
                if (user.getRoleId() == AppContext.ROLE_ACCOUNT_PROHIBIT_USE){
                    return Result.ACCOUNT_PROHIBIT_USE;
                }
                return Result.LOGIN_SUCCESS;
            }else {
                return Result.PASSWORD_ERROR;
            }
        }
        //用户不存在
        return Result.USER_DOES_NOT_EXIST;
    }

    /**
     * 用户注册
     * @param user1 账号
     * @return ·
     */
    @Override
    public Result userRegister(User user1) {
        User user = findUserByAccount(user1.getAccount());
        if (user == null){
            //用户账号不存在才进行注册
            user = user1;

            int userResult = userDao.addUser(user);
            //给注册用户添加一个默认详情
            user.getUserDetail().setUid(user.getUid());
            int detailResult = userDetailDao.addUserDetail(user.getUserDetail());
            //返回结果
            return userResult > 0 && detailResult > 0 ? Result.REGISTER_SUCCESS : Result.SERVER_BUSY;
        }else {
            return Result.REGISTER_FAIL;
        }
    }

    /**
     * 根据用户id查询用户详情信息
     * @param uid ·
     * @return ·
     */
    @Override
    public UserDetail findUserDetailByUid(long uid) {
        return userDetailDao.findUserDetailById(uid);
    }


    /**
     * 修改信息
     * @param userDetail ·
     * @return ·
     */
    @Override
    public Result updateUserDetail(UserDetail userDetail) {
        int result;
        //判断是否有该用户的用户详情
        if (userDetailDao.findUserDetailById(userDetail.getUid()) == null){
            result = userDetailDao.addUserDetail(userDetail);
        }else {
            result = userDetailDao.updateUserDetail(userDetail);
        }
        return result > 0 ? Result.UPDATE_SUCCESS : Result.UPDATE_FAIL;
    }

    @Override
    public PageResult<User> findAllUser(String account, String name, String sex, Integer pageNo) {
        //每页条数乘以当前页数减一
        int getData = AppContext.PAGE_COUNT * (pageNo - 1);
        //sql参数
        Map<String,Object> map = new HashMap<>();
        map.put("account","%"+account+"%");
        map.put("name","%"+name+"%");
        map.put("sex",sex);

        if (pageNo > 0) {
            map.put("getData",getData);
            map.put("pageCount",AppContext.PAGE_COUNT);
        }

        PageResult<User> pageResult = new PageResult<>();
        //获取集合
        List<User> allUser = userDao.findAllUser(map);
        pageResult.setPageNo(pageNo);
        pageResult.setData(allUser);
        pageResult.setMaxPageNo(userDao.findAllUserCount(map));

        return pageResult;
    }

    @Override
    public List<String> queryUserInfoColumnByKeyword(String column, String keyword) {
        return userDao.queryUserInfoColumnByKeyword(column,keyword);
    }
}
